#include "sxmem.h"

namespace sx
{

CTest MemTest;

SxSharedBuffer::SxSharedBuffer(unsigned int Size):
    mSize(Size), mRef()
{
    if (mSize)
    {
        mBasePtr = reinterpret_cast<void*>(new char[Size]);
        MemTest.incCount();
    }
    else
        mBasePtr = 0;

    mPtr = mBasePtr;
}

SxSharedBuffer::SxSharedBuffer(const SxSharedBuffer& Buff):
    mBasePtr(Buff.mBasePtr), mPtr(Buff.mPtr), mSize(Buff.mSize), mRef(Buff.mRef)
{
}

SxSharedBuffer::SxSharedBuffer(const SxSharedBuffer& Buff, int Offset):
    mBasePtr(Buff.mBasePtr), mPtr(0), mSize(Buff.mSize), mRef(Buff.mRef)
{
    setOffset(Offset);
}

SxSharedBuffer::~SxSharedBuffer()
{
    if (mRef == 1)
    {
        delete[] reinterpret_cast<char*>(mBasePtr);
        MemTest.decCount();
    }
    
}

SxSharedBuffer& SxSharedBuffer::operator= (const SxSharedBuffer& Buff)
{
    if (this != &Buff)
    {
        mBasePtr = Buff.mBasePtr;
        mRef = Buff.mRef;
        mPtr = Buff.mPtr;
        mSize = Buff.mSize;
    }
    return *this;
}

}  // namespace sx
